package com.lox;

import java.util.List;

class LoxFunction implements LoxCallable
{
    private final Stmt.Function declaration;
    LoxFunction(Stmt.Function declaration)
    {
        this.declaration = declaration;
    }
    //这几行代码是我们的解释器中最基本、最强大的部分之一。正如我们在上一章中所看到的，管理名称环境是语言实现中的核心部分。函数与此紧密相关。
    @Override
    public Object call(Interpreter interpreter,
                       List<Object> arguments) {
      Environment environment = new Environment(interpreter.globals);
      for (int i = 0; i < declaration.params.size(); i++) {
        environment.define(declaration.params.get(i).lexeme,
            arguments.get(i));
      }
  
      interpreter.executeBlock(declaration.body, environment);
      return null;
    }
}